package com.gr.staffpm.datatypes;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Proxy;


/**
 * Hibernate mapping class for Group Table
 * @author Graham Rhodes
 * @date   29 Oct 2010
 */
@Entity
@Proxy(lazy = false)
@Table(name = "groups")
public class Group implements Serializable {

    private static final long serialVersionUID = 1L;
    public static final String ID = "groupId";
    public static final String NAME = "name";
    private int groupId = 0;
    private String name = null;
    private Set<User> users = new HashSet<User>();
    private User createdBy = null;
    private Date created = new Date(Calendar.getInstance().getTimeInMillis());
    private User updatedBy = null;
    private Date lastUpdated = new Date(Calendar.getInstance().getTimeInMillis());

    @Id
    @GeneratedValue
    @Column(name = "group_id")
    public int getGroupId() {
        return groupId;
    }

    @Basic
    @Column(name = "name")
    public String getName() {
        return name;
    }

    @ManyToMany(targetEntity = User.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinTable(name = "users_groups", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "group_id"))
    public Set<User> getUsers() {
        return users;
    }

    @ManyToOne
    @JoinColumn(name = "createdBy")
    public User getCreatedBy() {
        return createdBy;
    }

    @Column(name = "created")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreated() {
        return created;
    }

    @ManyToOne
    @JoinColumn(name = "updatedBy")
    public User getUpdatedBy() {
        return updatedBy;
    }

    @Column(name = "lastUpdated")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getLastUpdated() {
        return lastUpdated;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public void setUpdatedBy(User updatedBy) {
        this.updatedBy = updatedBy;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }
}
